# -*- coding: utf-8 -*-

from odoo import models, fields, api, exceptions,_

class hr_attendance_month_report_wizard(models.TransientModel):
    _name = 'hr.attendance.month.report.wizard'
    _description = '考勤月报'
    department_id = fields.Many2one('hr.department',string=u'部门')
    employee_id=fields.Many2one('hr.employee', string=u'员工')
    # @api.depends('employee_id')
    # def compute_department_id(self):
    #     for r in self:
    #         r.department_id = r.employee_id.department_id
    date_time=fields.Date()
    def action_confirm(self):
        import calendar
        day_now=self.date_time.strftime('%Y-%m-%d')  # 给定日期
        day_begin = day_now[0:7] + '-01'  # 月初肯定是1号 所以 直接替换 就可以
        monthRange = calendar.monthrange(int(day_now[0:3]), int(day_now[5:7]))  # 得到本月的天数
        day_end = day_now[0:7] +'-' +str(monthRange[1])

        if self.department_id:
            employee_ids=self.env['hr.employee'].search([('department_id','=',self.department_id.id)])
            record_id=self.env['hr.attendance.wizard'].create({
                'start_date':day_begin,
                'end_date':day_end,
                'department_id':self.department_id.id
            })
        else:
            employee_ids=self.env['hr.employee'].search([])
            record_id=self.env['hr.attendance.wizard'].create({
                'start_date':day_begin,
                'end_date':day_end,
            })
        record_id.action_confirm()

        month_time=day_now[0:7]
        # if not self.department_id:
        #     employee_ids=self.env['hr.employee'].search([])
        # else:
        #     employee_ids=self.env['hr.employee'].search([('department_id','=',self.department_id.id)])
        #
        for employee_id in employee_ids:
            reports=self.env['hr.attendance.month.report']
            report_id=self.env['hr.attendance.month.report'].search([('employee_id','=',employee_id.id),('month_time','=',month_time)])
            if not report_id:
                self.env['hr.attendance.month.report'].create({
                    'employee_id':employee_id.id,
                    'department_id':employee_id.department_id.id,
                    'month_time':day_now[0:7],
                })
            overtime=employee_id.get_month_over_time(month_time)
            late_time=employee_id.get_month_late_time(month_time)
            absent_day=employee_id.get_month_absent_day(month_time)
            leave_early_time=employee_id.get_month_leave_early_time(month_time)
            leave_time=employee_id.get_month_leave_time(month_time)
            report_id.write({
                'overtime':overtime,
                'absent_day':absent_day,
                'leave_early_time':leave_early_time,
                'leave_time':leave_time,
                'late_time':late_time
            })
        reports|=report_id
        return {'type': 'ir.actions.act_window',
                'res_model': 'hr.attendance.month.report',
                'name': u'考勤月报表',
                'view_mode': 'tree',
                'domain': [('id', 'in', reports.ids)],
                'target': 'self',
                }



class hr_attendance_month_report(models.Model):
    _name = 'hr.attendance.month.report'
    _description = '考勤月报'
    department_id=fields.Many2one('hr.department',string=u'部门')
    employee_id=fields.Many2one('hr.employee',string=u'员工')
    month_time=fields.Char(string='月份')
    overtime=fields.Float(string=u'加班时间')
    late_time=fields.Float(string=u'迟到时间')
    absent_day=fields.Integer(string=u'旷工天数')
    leave_early_time=fields.Float(string=u'早退时间')
    leave_time=fields.Float(string=u'请假时间')